package com.javakc.copyrightin.bookList.service;

import com.javakc.commonutils.jpa.base.service.BaseService;
import com.javakc.commonutils.jpa.dynamic.SimpleSpecificationBuilder;
import com.javakc.copyrightin.bookList.dao.BookListDao;
import com.javakc.copyrightin.bookList.entity.BookList;
import com.javakc.copyrightin.bookList.vo.BookListQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class BookListService extends BaseService<BookListDao, BookList> {
    @Autowired
    private BookListDao bookListDao;

    /**
     * 查询所有书籍
     *
     * @return
     */
    public List<BookList> findAll() {
        return bookListDao.findAll();
    }

    /**
     * 根据条件进行分页查询
     * @param booklistQuery
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<BookList> findPage(int pageNo, int pageSize, BookListQuery booklistQuery) {
        /**
         * 可用操作符
         * = 等值、!= 不等值 (字符串、数字)
         * >=、<=、>、< (数字)
         * ge，le，gt，lt(字符串)
         * :表示like %v%
         * l:表示 v%
         * :l表示 %v
         * null表示 is null
         * !null表示 is not null
         */

        SimpleSpecificationBuilder simpleSpecificationBuilder = new SimpleSpecificationBuilder();
        if(!ObjectUtils.isEmpty(booklistQuery.getName())) {
            simpleSpecificationBuilder.and("name", ":", booklistQuery.getName());
        }
        if(!ObjectUtils.isEmpty(booklistQuery.getBatchId())) {
            simpleSpecificationBuilder.and("batchId", "=", booklistQuery.getBatchId());
        }
        Page<BookList> page = dao.findAll(simpleSpecificationBuilder.getSpecification(), PageRequest.of(pageNo - 1, pageSize));
        return page;
    }
}
